/*
 * @Autor: dc
 * @Date: 2022-03-30 17:31:30
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-10-18 16:04:29
 * @Description: 主场景页面
 */
import React, { Component } from 'react';
import store from '../../redux/store'
import { Toast } from 'antd-mobile'

// 页面样式
import './index.css'
// 一般组件
import UserState from '../../components/UserState'
import WorkStation from '../../components/WorkStation'
import Tree from '../../components/Tree'
import Cloud from '../../components/Cloud'
import { Routes } from 'react-router';

class index extends Component {
    constructor(props) {
        super(props);

        this.state = {};
        // 操作
        this.sunlightRef = React.createRef();
        this.diggingRef = React.createRef();
        this.fertilizationRef = React.createRef();
        this.wateringRef = React.createRef();

    }
    // 操作方法
    operationFun = (type) => {
        const { isOperation, operationArr } = store.getState();
        const { prepare } = operationArr.find(item => item.name === type)

        if (!isOperation || prepare > 0) {
            Toast.show({
                content: '正在操作,请稍后',
                maskClickable: false,
            })
            return
        }
        store.dispatch({
            type: 'operationBoolean',
            data: false,
        })
        switch (type) {
            case '阳光':
                this.sunlightRef.sunlightFun()
                break
            case '松土':
                this.diggingRef.diggingFun()
                break
            case '施肥':
                this.fertilizationRef.fertilizerFun()
                break
            case '浇水':
                this.wateringRef.wateringFun()
                break
            default:
                break;
        }
    }
    // 阳光ref
    onRefSunlight = (ref) => {
        this.sunlightRef = ref
    }
    // 松土ref
    onRefDigging = (ref) => {
        this.diggingRef = ref
    }
    // 施肥ref
    onRefFertilization = (ref) => {
        this.fertilizationRef = ref
    }
    // 浇水ref
    onRefWatering = (ref) => {
        this.wateringRef = ref
    }
    render() {

        return (
            <div id='index'>
                {/* 用户状态 */}
                <UserState ></UserState>

                {/* 云 */}
                <Cloud></Cloud>

                {/* 树 */}
                <Tree
                    onRefSunlight={this.onRefSunlight}
                    onRefDigging={this.onRefDigging}
                    onRefFertilization={this.onRefFertilization}
                    onRefWatering={this.onRefWatering}
                ></Tree>

                {/* 操作台 */}
                <WorkStation operationFun={type => this.operationFun(type)}></WorkStation>
            </div>
        );
    }
}

export default (index);